<template>
  <van-icon :color="collected?'red':'#777'" :name="collected?'star':'star-o'" @click='oncollected' />
</template>

<script>
// 收藏文章请求 取消收藏文章请求
import { addArticleCollected, deleteArticleCollected } from '@/api/article.js'
export default {
  data () {
    return {}
  },
  // 修改v-model解析
  model: {
    prop: 'collected'
  },
  // 父传子收藏状态
  props: {
    collected: {
      type: Boolean,
      required: true
    },
    // 父传子文章ID
    articleId: {
      type: [Object, Number],
      required: true
    }
  },
  methods: {
    // 点击收藏按钮
    async oncollected () {
      // 轻提示加载状态
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0 // 持续展示 toast
      })
      try {
        if (this.collected) {
          await deleteArticleCollected(this.articleId)
          this.$toast('取消收藏')
        } else {
          await addArticleCollected(this.articleId)
          this.$toast('收藏成功')
        }
        this.$emit('input', !this.collected)
      } catch (err) {
        this.$toast('操作失败')
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
